﻿using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace YArchitech.Controls
{
	public class ToolTipListBox : ListBox
	{
		[DllImport("gdi32.dll")]
		public static extern int GetTextExtentPoint32(IntPtr hdc, string str, int len, ref ToolTipListBox.SIZE size);

		[DllImport("user32.dll")]
		public static extern IntPtr GetDC(IntPtr hWnd);

		[DllImport("user32.dll")]
		public static extern int ReleaseDC(IntPtr hWnd, IntPtr hdc);

		public ToolTipListBox()
		{
			this.tip = new ToolTip();
			this.tip.InitialDelay = 1000;
			this.tip.AutoPopDelay = 2000;
			this.tip.Active = true;
		}

		private void SetTipMessage(string strTip)
		{
			this.tip.SetToolTip(this, strTip);
		}

		protected override void OnMouseMove(MouseEventArgs e)
		{
			base.OnMouseMove(e);
			int num = base.IndexFromPoint(e.Location);
			if (num == -1)
			{
				this.SetTipMessage("");
				return;
			}
			if (num != this.oldIndex)
			{
				this.oldIndex = num;
				string itemText = base.GetItemText(base.Items[num]);
				this.SetTipMessage(itemText);
			}
		}

		private ToolTip tip;

		private int oldIndex = -1;

		public struct SIZE
		{
			public int cx;

			public int cy;
		}
	}
}
